package com.lx.oms.controller.controller;

import java.math.BigInteger;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lx.oms.common.annotation.SysLog;
import com.lx.oms.common.controller.AbstractController;
import com.lx.oms.common.entity.Page;
import com.lx.oms.common.entity.R;
import com.lx.oms.common.utils.ImageOperation;
import com.lx.oms.controller.entity.WCategoryEntity;
import com.lx.oms.controller.service.WCategoryService;

/**
 * 微信类别
 *
 * @author houzhong
 * @date 2018年8月06日 上午11:40:32
 */
@RestController
@RequestMapping("/sys/wcategory")
public class WCategoryController extends AbstractController {
	
	@Autowired
	private WCategoryService wCategoryService;
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	public Page<WCategoryEntity> list(@RequestBody Map<String, Object> params) {
		return wCategoryService.listWCategory(params);
	}
		
	/**
	 * 新增
	 * @param wCategory
	 * @return
	 */
	@SysLog("新增微信类别")
	@RequestMapping("/save")
	public R save(@RequestBody WCategoryEntity wCategory,HttpServletRequest request) {
		wCategory.setParentId("0");
		
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		//解析base64位，压缩图片
		String yzmImagePath = ImageOperation.encode64Image(wCategory.getImg(),url);
		//获取图片路径
		wCategory.setImg(yzmImagePath);
		return wCategoryService.saveWCategory(wCategory);
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/info")
	public R getById(@RequestBody String id) {
		BigInteger bId = new BigInteger(id);
		return wCategoryService.getWCategoryById(bId);
	}
	
	/**
	 * 修改
	 * @param wCategory
	 * @return
	 */
	@SysLog("修改微信类别")
	@RequestMapping("/update")
	public R update(@RequestBody WCategoryEntity wCategory,HttpServletRequest request) {
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		String [] d = wCategory.getImg().split("base64,");
        if(d != null && d.length == 2){
            //解析base64位，压缩图片
    		String yzmImagePath = ImageOperation.encode64Image(wCategory.getImg(),url);
    		//获取图片路径
    		wCategory.setImg(yzmImagePath);
        }else {
        	wCategory.setImg(wCategory.getImg().substring(wCategory.getImg().lastIndexOf("/")+1));
        }
		return wCategoryService.updateWCategory(wCategory);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除微信类别")
	@RequestMapping("/remove")
	public R batchRemove(@RequestBody String[] id) {
		return wCategoryService.batchRemove(id);
	}
	
}
